Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic plugin support #196

Closed
wants to merge 1 commit into from
Closed

feat: basic plugin support #196

wants to merge 1 commit into from

Conversation

mdaffin
Copy link
Contributor

@mdaffin mdaffin commented Apr 21, 2018

Experimental basic plugin support to allow the app and themes to extend the functionality of vuepress. It attempts to load themeDir/index.js then .vuepress/index.js and gives them access to the options at the end of the prepare method. Plugins can then use this to write extra files such as to write a _redirects file to support redirects in netlify:

const path = require('path')
const fs = require('fs-extra')

module.exports = async function ({options}) {
  const redirects = options.siteData.pages
    .filter(page => page.frontmatter.aliases)
    .map(page => page.frontmatter.aliases.map(alias => ({
        dest: page.path,
        alias: alias
      })))
    .reduce((acc, aliases) => acc.concat(aliases), [])
    .map(({dest, alias}) => `${alias}\t${dest}`)

  if (redirects.length > 0) {
    await fs.writeFile(
        path.resolve(options.outDir, '_redirects'),
        redirects.join('\n')
    )
  }
}

This can also be used for RSS feeds, sitemaps and anything else that needs to be dynamically written based on the posts or config data.

This also allows plugins to modify the options data to inject extra values or config like markdown-it plugins of header metadata.

This currently conflicts with pull request #154 as they both use themeDir/index.js for different purposes. I think it is better to use the index.js for plugins and rename the file in #154 but we could also use an alternative file like plugin.js (or just drop the pull request as it should be possible to load server side components via the plugin).

@ycmjason
Copy link
Contributor

ycmjason commented Apr 22, 2018

I really like this idea ♥️

@yyx990803 yyx990803 added the type: question or discussion Question or discussion label Apr 23, 2018
@mdaffin mdaffin closed this Apr 24, 2018
@mdaffin mdaffin deleted the server-side-plugin branch April 25, 2018 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Question or discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants